#ifndef FKCLIENTMANAGER_H
#define FKCLIENTMANAGER_H

#include <QObject>

#include <QTimer>
#include <QVariant>

class QTcpSocket;
class FKClient;
class FKEvent;
class FKFileSender;

class FKClientManager : public QObject{
    Q_OBJECT
public:
    FKClientManager(QTcpSocket* socket=0, QObject *parent = 0);
    ~FKClientManager();
    QTcpSocket* socket();
    FKClient* client();
    void setClient(FKClient* client);
    void startDeletingTimer(int latency=60000);
    void stopDeletingTimer();
    void sendData(QByteArray data);
//    void setFileSender(FKFileSender* s);

    static QByteArray preserveData(QByteArray data);
public slots:
    void sendMessage(QString message);
    void sendFileMessage(QVariant message);
    void sendInterruptFileMessage(QString message);
signals:
    void disconnected(FKClientManager* manager);
    void genClientRequested(QString clientName, QString password, QVariant validator, FKClientManager* manager);
    void clientRequested(QString clientName, QString password, FKClientManager* manager);
    void gotAction(FKEvent* action);
    void gotEvent(FKEvent* event);
    void clientSetted(FKClient* client);
    void message(QString message);
    void message(QString message, FKClientManager* source);
    void fileMessage(QVariant var);
    void interruptFileMessage(QString file);
private slots:
    void readData();
    void socketDisconnected();
private:
    bool readMessageData();
    bool readBlockSize();
    void parseMessageData(QByteArray data);
private:
    QTcpSocket* _socket;
    FKClient* _client;
    qint32 _blockSize;
    QTimer _deletingTimer;
    FKFileSender* _sender;
};

#endif // FKCLIENTMANAGER_H
